﻿using System;
using System.Collections.Generic;
using System.Text;
using TomShane.Neoforce.Controls;
using RebEngine.Managers;
using Microsoft.Xna.Framework.Graphics;
using RebellionII.GameObjects.Map;

namespace RebellionII.GUI.Parts
{
    public class RebUnitMoveScrap
    {
        public GroupPanel GroupPanel { get; set; }
        public Button ScrapButton { get; set; }
        public Button AddToMoveButton { get; set; }
        public TextBox SelectedNumberText { get; set; }

        public int Reference { get; private set; }
        public RebSystem TheSystem { get; private set; }

        public Label numOf { get; private set; }

        public RebUnitMoveScrap(Control namedControl, int left, int top, int numOfUnit, int UnitReferenceID, RebSystem rebsys)
        {
            TheSystem = rebsys;
            Reference = UnitReferenceID;
            GroupPanel = new GroupPanel(InputManager.Manager);
            GroupPanel.Init();
            GroupPanel.Parent = namedControl;
            GroupPanel.Anchor = Anchors.Left | Anchors.Top | Anchors.Right;
            GroupPanel.Width = 187;
            GroupPanel.Height = 63;
            GroupPanel.Left = left;
            GroupPanel.Top = top;
            GroupPanel.Text = NetClientManager.UnitList[UnitReferenceID].sName;

            ImageBox img = new ImageBox(InputManager.Manager);
            img.Init();
            img.Width = 45;
            img.Height = 22;
            img.Left = 25;
            img.Top = 8;
            img.Image = TextureManager.GetTexture(NetClientManager.UnitList[UnitReferenceID].sTextureName).BaseTexture as Texture2D;
            img.SizeMode = SizeMode.Stretched;
            GroupPanel.Add(img);

            numOf = new Label(InputManager.Manager);
            numOf.Init();
            numOf.Anchor = Anchors.Left | Anchors.Top;
            numOf.Left = 5;
            numOf.Top = 13;
            numOf.Width = 50;
            numOf.Height = 10;
            numOf.Text = numOfUnit.ToString() + " x ";
            GroupPanel.Add(numOf);

            SelectedNumberText = new TextBox(InputManager.Manager);
            SelectedNumberText.Init();
            SelectedNumberText.Anchor = Anchors.Top | Anchors.Right;
            SelectedNumberText.Left = GroupPanel.Width / 2 - 19;
            SelectedNumberText.Top = 8;
            SelectedNumberText.Width = 23;
            SelectedNumberText.Height = 25;
            SelectedNumberText.Text = "0";
            GroupPanel.Add(SelectedNumberText);

            ScrapButton = new Button(InputManager.Manager);
            ScrapButton.Init();
            ScrapButton.Left = 100;
            ScrapButton.Width = 40;
            ScrapButton.Top = 6;
            ScrapButton.Height = 30;
            ScrapButton.Text = "Scrap";
            ScrapButton.ToolTip.Text = "This will destroy the number of units of this type you choose.";
            ScrapButton.Click += new TomShane.Neoforce.Controls.EventHandler(ScrapButton_Click);
            GroupPanel.Add(ScrapButton);

            AddToMoveButton = new Button(InputManager.Manager);
            AddToMoveButton.Init();
            AddToMoveButton.Left = 142;
            AddToMoveButton.Width = 40;
            AddToMoveButton.Top = 6;
            AddToMoveButton.Height = 30;
            AddToMoveButton.Text = "Move";
            AddToMoveButton.ToolTip.Text = "This will add the number of units to the Move Panel.";
            GroupPanel.Add(AddToMoveButton);
        }

        void ScrapButton_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            //Check if sure, then
            List<int> instances = new List<int>();
            int numRemove = Convert.ToInt32(SelectedNumberText.Text); 

            switch (NetClientManager.UnitList[Reference].GetType().Name.ToString())
            {
                case "Troop":
                        for(int j = 0; j < TheSystem.lTroop.Count; j++)
                        {
                            if ((TheSystem.lTroop[j].iReferenceID == Reference) && (numRemove != 0))
                            {
                                TheSystem.lTroop.RemoveAt(j);
                                j--;
                                numRemove--;
                            }
                        }
                    
                    break;
                case "Facility":
                    for (int j = 0; j < TheSystem.lFacilities.Count; j++)
                    {
                        if ((TheSystem.lFacilities[j].iReferenceID == Reference) && (numRemove != 0))
                        {
                            TheSystem.lFacilities.RemoveAt(j);
                            j--;
                            numRemove--;
                        }
                    }
                    break;
                case "Fighter":
                    for (int j = 0; j < TheSystem.lFighter.Count; j++)
                    {
                        if ((TheSystem.lFighter[j].iReferenceID == Reference) && (numRemove != 0))
                        {
                            TheSystem.lFighter.RemoveAt(j);
                            j--;
                            numRemove--;
                        }
                    }
                    break;
            }
            UpdateMoveScrap();  
        }

        private void UpdateMoveScrap()
        {
            SelectedNumberText.Text = "0";
            switch (NetClientManager.UnitList[Reference].GetType().Name.ToString())
            {
                case "Troop":
                    int count = 0;
                    foreach(var temp in TheSystem.lTroop)
                    {
                        if(temp.GetReferenceID == Reference)
                            count++;
                    }
                    numOf.Text = count.ToString() + "  x ";
                    if (count == 0)
                        GroupPanel.Parent.Remove(GroupPanel);
                      
                    break;
                case "Facility":
                    count = 0;
                    foreach (var temp in TheSystem.lFacilities)
                    {
                        if (temp.GetReferenceID == Reference)
                            count++;
                    }
                    numOf.Text = count.ToString() + "  x ";
                    if (count == 0)
                        GroupPanel.Parent.Remove(GroupPanel);
                    break;
                case "Fighter":
                    count = 0;
                    foreach (var temp in TheSystem.lFighter)
                    {
                        if (temp.GetReferenceID == Reference)
                            count++;
                    }
                    numOf.Text = count.ToString() + "  x ";
                    if (count == 0)
                        GroupPanel.Parent.Remove(GroupPanel);
                    break;
            }
        }
    }
}
